package com.huaruan.youdi.views.fragments;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.huaruan.youdi.R;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.models.MineModel;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.UserUtils;
import com.huaruan.youdi.views.WHImageView;
import com.huaruan.youdi.views.activitys.AppealListActivity;
import com.huaruan.youdi.views.activitys.HistoryActivity;
import com.huaruan.youdi.views.activitys.SettingActivity;

import butterknife.BindView;

public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_avatar)
    WHImageView iv_avatar;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.ll_appeal)
    LinearLayout ll_appeal;
    @BindView(R.id.ll_history)
    LinearLayout ll_history;
    @BindView(R.id.ll_set)
    LinearLayout ll_set;


    @Override
    public int IGetContentViewResId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void IFindViews(final View view) {
        ll_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtils.jump(IGetActivity(), HistoryActivity.class);
            }
        });

        ll_appeal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtils.jump(IGetActivity(), AppealListActivity.class);
            }
        });

        ll_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtils.jump(IGetActivity(), SettingActivity.class);
            }
        });
    }


    @Override
    public void IInitData() {
    }

    private void initData(MineModel model) {
        iv_avatar.loadLocalCirclePic(R.mipmap.icon_avatar_default);
        tv_name.setText(model.name);
        tv_id.setText(model.phone);
    }

    private void queryUserInfo() {
        String url = Constant.MINE + UserUtils.getUid();
        new HttpUtils.Builder(IGetContext())
                .url(url)
                .modelClass(MineModel.class)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        MineModel model = (MineModel) object;
                        initData(model);
                    }
                }).start();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser)
            queryUserInfo();
    }

}
